<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<!DOCTYPE html>
<html>
<head>
    <meta charset="utf-8">
    <title>员工管理</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
</head>
<body>
<div class="layuimini-container">
    <div class="layuimini-main">

        <fieldset class="table-search-fieldset">
            <legend>搜索信息</legend>
            <div style="margin: 10px 10px 10px 10px">
                <div class="layui-form layui-form-pane">
                    <div class="layui-form-item">
                        <div class="layui-inline">
                            <label class="layui-form-label">员工编号</label>
                            <div class="layui-input-inline">
                                <input id="empno" type="text" name="empno" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">员工姓名</label>
                            <div class="layui-input-inline">
                                <input id="ename" type="text" name="ename" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">职位</label>
                            <div class="layui-input-inline">
                                <input id="role" type="text" name="role" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <label class="layui-form-label">部门</label>
                            <div class="layui-input-inline">
                                <input id="dname" type="text" name="dname" autocomplete="off" class="layui-input">
                            </div>
                        </div>
                        <div class="layui-inline">
                            <button type="submit" class="layui-btn layui-btn-primary"  lay-submit lay-filter="data-search-btn"><i class="layui-icon"></i> 搜 索</button>
                        </div>
                    </div>
                </div>
            </div>
        </fieldset>

        <script type="text/html" id="toolbarDemo">
            <div class="layui-btn-container">
                <button class="layui-btn layui-btn-normal layui-btn-sm data-add-btn" lay-event="add"> 添加 </button>
            </div>
        </script>

        <table class="layui-hide" id="currentTableId" lay-filter="currentTableFilter"></table>

        <script type="text/html" id="currentTableBar">
            <a class="layui-btn layui-btn-normal layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
            <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">删除</a>
        </script>
        <script type="text/html" id="sexTpl">
            {{#  if(d.sex === '女'){ }}
            <span style="color: #F581B1;">{{ d.sex }}</span>
            {{#  } else { }}
            {{ d.sex }}
            {{#  } }}
        </script>

    </div>
</div>
<script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
<script src="../../../../jquery-3.3.1.js" charset="UTF-8"></script>
<script>
    layui.use(['form', 'table'], function () {
        var $ = layui.jquery,
            form = layui.form,
            table = layui.table;

        function add0(m){
            return m<10?'0'+m:m
        }

        table.render({
            elem: '#currentTableId',
            url: '/emp/list',
            toolbar: '#toolbarDemo',
            defaultToolbar: ['filter', 'exports', 'print', {
                title: '提示',
                layEvent: 'LAYTABLE_TIPS',
                icon: 'layui-icon-tips'
            }],
            cols: [[
                {width:80,},
                {field: 'empno', width: 100, title: '员工编号', sort: true},
                {field: 'ename', width: 90, title: '姓名'},
                {field: 'sex', width: 90, title: '性别',templet: '#sexTpl'},
                {field: 'role', width: 90, title: '职位',templet:function (r) {
                        return r.roles.role
                    }},
                {field: 'idcard',width: 200, title: '身份证号'},
                {field: 'phone', width: 200, title: '手机号', sort: true},
                {field: 'hiredate', width: 160, title: '入职日期', sort: true,templet:function (d) {
                        var time = new Date(d.hiredate);
                        var y = time.getFullYear();
                        var m = time.getMonth()+1;
                        var d = time.getDate();
                        return y+'-'+add0(m)+'-'+add0(d);
                    }},
                {field: 'mgr', width: 90, title: '领导'},
                {field: 'sal', width: 90, title: '月薪', sort: true},
                {field: 'comm', width: 90, title: '奖金', sort: true},
                {field: 'dname', width:90, title: '部门', sort: true,templet:function (d) {
                       return d.deptnos.dname
                    }},
                {field: 'loc', width:120, title: '位置', sort: true,templet:function (d) {
                        return d.deptnos.loc
                    }},
                {title: '操作', minWidth: 150, toolbar: '#currentTableBar', align: "center"}
            ]], page:true,
            limits: [5,10, 15]
            , limit: 10
            , parseData: function (res) { //将原始数据解析成 table 组件所规定的数据，res为从url中get到的数据
                var result;
                if (this.page.curr) {
                    result = res.data.slice(this.limit * (this.page.curr - 1), this.limit * this.page.curr);
                }
                else {
                    result = res.data.slice(0, this.limit);
                }
                return {
                    "code": res.code, //解析接口状态
                    "msg": res.msg, //解析提示文本
                    "count": res.count, //解析数据长度
                    "data": result //解析数据列表
                };
            }
        });

        // 监听搜索操作
        form.on('submit(data-search-btn)', function (data) {
            var result = JSON.stringify(data.field);
            //执行搜索重载
            table.reload('currentTableId', {
                page: {
                    curr: 1
                }
                , where: {
                    empno:$("#empno").val(),
                    ename:$("#ename").val(),
                    role:$("#role").val(),
                    dname:$("#dname").val(),
                }
            }, 'data');
            return false;
        });

        /**
         * toolbar监听事件
         */
        table.on('toolbar(currentTableFilter)', function (obj) {
            if (obj.event === 'add') {  // 监听添加操作
                var index = layer.open({
                    title: '添加用户',
                    type: 2,
                    shade: 0.2,
                    maxmin:true,
                    shadeClose: true,
                    area: ['100%', '100%'],
                    content: 'add.jsp',
                    end:function () {
                        location.reload()
                    },

                });
                $(window).on("resize", function () {
                    layer.full(index);
                });
            } else if (obj.event === 'delete') {  // 监听删除操作
                var checkStatus = table.checkStatus('currentTableId')
                    , data = checkStatus.data;
                layer.alert(JSON.stringify(data));
            }
        });


        table.on('tool(currentTableFilter)', function (obj) {
            var data = obj.data;
            if (obj.event === 'edit') {
                        var index = layer.open({
                            title: '编辑用户',
                            type: 2,
                            shade: 0.2,
                            maxmin:true,
                            shadeClose: true,
                            area: ['100%', '100%'],
                            content: 'edit.jsp',
                            success:function(layero,index){
                                function toString (timestamp) {
                                    var date = new Date(parseInt(timestamp));// 时间戳为10位需*1000，时间戳为13位的话不需乘1000
                                    var Y = date.getFullYear() + '-';
                                    var M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
                                    var D = (date.getDate() + 1 < 10 ? '0' + (date.getDate() + 1) : date.getDate() + 1);
                                    return Y + M + D;
                                }
                                var body=layer.getChildFrame('body',index);
                                body.contents().find("#empno").val(data.empno);
                                body.contents().find("#ename").val(data.ename);
                                body.find("input[name=sex][value=男]").attr("checked", data.sex == "男" ? true : false);
                                body.find("input[name=sex][value=女]").attr("checked", data.sex == "女" ? true : false);
                                body.contents().find("#idcard").val(data.idcard);
                                body.contents().find("#phone").val(data.phone);
                                body.contents().find("#hiredate").val(toString(data.hiredate));
                                body.contents().find("#mgr").val(data.mgr);
                                body.contents().find("#sal").val(data.sal);
                                body.contents().find("#comm").val(data.comm);
                            },
                            end:function () {
                                location.reload()
                            },
                        });
                        $(window).on("resize", function () {
                            layer.full(index);
                        });
                return false;
            } else if (obj.event === 'delete') {
                layer.confirm('真的删除行么', function (index) {
                    $.post(
                        "/emp/del",{
                            empno:data.empno
                        },function () {
                            layer.close(index);
                            window.location.reload()
                        }
                    )
                });
            }
        });

    });
</script>

</body>
</html>